package com.newmainsoftech.ant.script;

import org.apache.tools.ant.taskdefs.Java;
import org.apache.tools.ant.types.Commandline.Argument;
import org.apache.tools.ant.types.LogLevel;
import org.apache.tools.ant.types.Path;

import com.newmainsoftech.ant.script.ReadMultiLineArgs.ArgPrefix;

public class JavaTaskUtil extends AntrunScriptUtilTemplate {
	private Java javaTask;
		public synchronized Java getJavaTask() {
			return javaTask;
		}
		public synchronized void setJavaTask( Java javaTask) {
			this.javaTask = javaTask;
		}
		
	enum ArgType {
		JvmArg, JavaArg;
	}
	void addArg( final Java javaTask, final String arg, final ArgType argType) {
		if ( javaTask == null) {
			throw new IllegalArgumentException( "javaTask input cannot be null.");
		}
		if ( arg == null) {
			throw new IllegalArgumentException( "arg input cannot be null.");
		}
		
		String javaTaskName = javaTask.getTaskName();
			if ( javaTaskName == null) javaTaskName = "";
		String argcCopy = arg.trim();
		if ( argcCopy.length() < 1) {
			String format 
			= String.format( 
					"Skiped to add arg input as an argument to java task%1$s because " 
					+ "arg input does not hold substantial string value."
					);
			javaTask.log( 
					String.format(
							"Skiped to add arg input as an argument to java task%1$s because " 
							+ "arg input does not hold substantial string value.", 
							((javaTaskName.length() < 1) ? "" : " (named as " + javaTaskName + ")")
							),
					LogLevel.WARN.getLevel()
					);
			return;
		}
		
		Argument argumentObj = javaTask.createArg();
		argumentObj.setValue( argcCopy);
		javaTask.log(
				String.format( 
						"Added next argument to java task %1$s: %2$s",
						((javaTaskName.length() < 1) ? "" : "(named as " + javaTaskName + ") "),
						argcCopy
						), 
				LogLevel.DEBUG.getLevel()
				);
	}
	
	public void addArg( final Java javaTask, final String arg) {
		if ( javaTask == null) {
			throw new IllegalArgumentException( "javaTask input cannot be null.");
		}
		if ( arg == null) {
			throw new IllegalArgumentException( "arg input cannot be null.");
		}
		
		String javaTaskName = javaTask.getTaskName();
			if ( javaTaskName == null) javaTaskName = "";
		String argcCopy = arg.trim();
		if ( argcCopy.length() < 1) {
			javaTask.log( 
					String.format(
							"Skiped to add arg input as an argument to java task%1$s because " 
							+ "arg input does not hold substantial string value.", 
							((javaTaskName.length() < 1) ? "" : " (named as " + javaTaskName + ")")
							),
					LogLevel.WARN.getLevel()
					);
			return;
		}
		Argument argumentObj = javaTask.createArg();
		argumentObj.setValue( argcCopy);
		javaTask.log(
				String.format( 
						"Added next argument to java task %1$s: %2$s",
						((javaTaskName.length() < 1) ? "" : "(named as " + javaTaskName + ") "),
						argcCopy
						), 
				LogLevel.DEBUG.getLevel()
				);
	}
	public void addArg( final String arg) {
		addArg( getJavaTask(), arg);
	}
	public void addArgs( final Java javaTask, final String args, final ArgPrefix argPrefix) {
		if ( javaTask == null) {
			throw new IllegalArgumentException( "javaTask input cannot be null.");
		}
		if ( args == null) {
			throw new IllegalArgumentException( "arg input cannot be null.");
		}
		
		String javaTaskName = javaTask.getTaskName();
			if ( javaTaskName == null) javaTaskName = "";
		
		if ( args.trim().length() < 1) {
			javaTask.log( 
					String.format(
							"Skiped to add arguments from args input to java task%1$s because " 
							+ "args input does not hold substantial string values.", 
							((javaTaskName.length() < 1) ? "" : " (named as " + javaTaskName + ")")
							),
					LogLevel.WARN.getLevel()
					);
			return;
		}
		
		ReadMultiLineArgs readMultiLineArgs = new ReadMultiLineArgs();
		for( String arg : readMultiLineArgs.splitByPrefix(args, argPrefix)) {
			Argument argument = javaTask.createArg();
			argument.setValue( arg);
			javaTask.log( 
					String.format( 
							"Added next argument to java task %1$s: %2$s",
							((javaTaskName.length() < 1) ? "" : "(named as " + javaTaskName + ") "),
							arg
							), 
					LogLevel.DEBUG.getLevel());
		} // for
	}
	public void addArgsAsPathFormat( final Java javaTask, final String args, final ArgPrefix argPrefix) {
		if ( javaTask == null) {
			throw new IllegalArgumentException( "javaTask input cannot be null.");
		}
		if ( args == null) {
			throw new IllegalArgumentException( "arg input cannot be null.");
		}
		
		String javaTaskName = javaTask.getTaskName();
			if ( javaTaskName == null) javaTaskName = "";
	
		ReadMultiLineArgs readMultiLineArgs = new ReadMultiLineArgs();
		String concatenatedArgs = readMultiLineArgs.concatinate( args, argPrefix, ";");
			if ( concatenatedArgs.length() < 1) {
				javaTask.log( 
						String.format(
								"Could not find an argument with %1$s prefix in args input to add to " 
								+ "java task%1$s.", 
								((argPrefix == null) ? "space" : argPrefix.getPrefix()), 
								((javaTaskName.length() < 1) ? "" : " (named as " + javaTaskName + ")")
								),
						LogLevel.WARN.getLevel()
						);
				return;
			}
		
		Path argsPath = new Path( javaTask.getProject(), concatenatedArgs);
		Argument argument = javaTask.createArg();
		argument.setPath( argsPath);
		javaTask.log( 
				String.format( 
						"Added next arguments in path format to java task %1$s: %2$s",
						((javaTaskName.length() < 1) ? "" : "(named as " + javaTaskName + ") "),
						concatenatedArgs
						), 
				LogLevel.DEBUG.getLevel());
	}
}
